package org.xshop.web;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.xshop.core.ExpressmanHelper;
import org.xshop.model.TOrderExpress;
import org.xshop.service.ITOrderExpressService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.exception.BusinessException;
import top.ibase4j.core.util.InstanceUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * @author ShenHuaJie
 * @since 2018年5月31日 上午11:06:01
 */
@Controller
@RequestMapping("/app/expressman/order/")
@Api(tags = "ExpressOrder", description = "订单派送")
public class TOrderExpressController extends AppBaseController<TOrderExpress, ITOrderExpressService> {
    @ApiOperation(value = "可接订单查询", notes = "参数：orderBy(store取货距离,address派送距离,money派送费),lon经度,lat纬度")
    @RequestMapping(value = "available.api", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<ModelMap> queryAvailableOrder(HttpServletRequest request, String orderBy) {
        Map<String, Object> params = WebUtil.getParameter(request);
        try {
            params.put("expressmanId", ExpressmanHelper.getCurrent(request));
        } catch (Exception e) {
        }
        Object result = service.queryAvailableOrder(params);
        return setSuccessModelMap(result);
    }

    @PostMapping("/want.api")
    @ApiOperation("抢单")
    public Object wantOrder(HttpServletRequest request, Long orderId) {
        TOrderExpress param = WebUtil.getParameter(request, TOrderExpress.class);
        TOrderExpress orderExpress = new TOrderExpress();
        orderExpress.setOrderId(param.getOrderId());
        orderExpress.setExpressmanId(ExpressmanHelper.getCurrent(request));
        orderExpress.setState(0);
        orderExpress = service.update(orderExpress);
        Map<String, Object> params = InstanceUtil.newHashMap("state", 1);
        params.put("expressmanId", ExpressmanHelper.getCurrent(request));
        Object result = service.queryOrder(params);
        return setSuccessModelMap(result);
    }

    @ApiOperation(value = "我的派送单", notes = "参数：state(0待取货，1待送达，2完成),lon经度,lat纬度")
    @RequestMapping(value = "my.api", method = {RequestMethod.GET, RequestMethod.POST})
    public Object myOrder(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        param.put("expressmanId", ExpressmanHelper.getCurrent(request));
        Object result = service.queryOrder(param);
        return setSuccessModelMap(result);
    }

    @PostMapping("/update.api")
    @ApiOperation(value = "修改派送单状态", notes = "参数：orderId订单id")
    public Object updateOrder(HttpServletRequest request, Long orderId, Integer state, BigDecimal lon, BigDecimal lat) {
        TOrderExpress param = WebUtil.getParameter(request, TOrderExpress.class);
        TOrderExpress entity = new TOrderExpress();
        entity.setOrderId(param.getOrderId());
        TOrderExpress orderExpress = service.selectOne(entity);
        if (!ExpressmanHelper.getCurrent(request).equals(orderExpress.getExpressmanId())) {
            throw new BusinessException("您没有权限进行该操作");
        }
        if (param.getState() - orderExpress.getState() != 1) {
            throw new BusinessException("派送单状态异常,请刷新重试");
        }
        orderExpress.setState(param.getState());
        orderExpress.setLon(param.getLon());
        orderExpress.setLat(param.getLat());
        service.update(orderExpress);
        return setSuccessModelMap();
    }

    @ApiOperation(value = "订单坐标(热力图)", notes = "lon经度,lat纬度")
    @RequestMapping(value = "orderChar.api", method = {RequestMethod.GET, RequestMethod.POST})
    public Object orderChar(HttpServletRequest request, BigDecimal lon, BigDecimal lat) {
        Map<String, Object> param = WebUtil.getParameter(request);
        Object result = service.getOrderChart(param);
        return setSuccessModelMap(result);
    }
}
